package com.nanhai.buddhist.college.module.collegebase.dal.mysql.colleges;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.datapermission.core.annotation.DataPermission;
import com.nanhai.buddhist.college.framework.mybatis.core.mapper.BaseMapperX;
import com.nanhai.buddhist.college.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.nanhai.buddhist.college.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.colleges.vo.CollegesExportReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.colleges.vo.CollegesPageReqVO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.classes.ClassesDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.classes.ClassesInfoDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.colleges.CollegesDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.departments.DepartmentsDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.professions.ProfessionsDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.List;

/**
 * 学校管理--学院 Mapper
 *
 * @author 开发
 */
@Mapper
public interface CollegesMapper extends BaseMapperX<CollegesDO> {

    default PageResult<CollegesDO> selectPage(CollegesPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CollegesDO>()
                .eqIfPresent(CollegesDO::getCollegeNumber, reqVO.getCollegeNumber())
                .likeIfPresent(CollegesDO::getCollegeName, reqVO.getCollegeName())
                .geIfPresent(CollegesDO::getOpeningTime,reqVO.getOpeningStartTimeDate())
                .leIfPresent(CollegesDO::getOpeningTime,reqVO.getOpeningEndTimeDate())
                .eqIfPresent(CollegesDO::getCollegeCode, reqVO.getCollegeCode())
                .likeIfPresent(CollegesDO::getUserId, reqVO.getUserId())
                .betweenIfPresent(CollegesDO::getOpeningTime, reqVO.getOpeningTime())
                .orderByDesc(CollegesDO::getCreateTime));
    }

    default List<CollegesDO> selectList(CollegesExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<CollegesDO>()
                .eqIfPresent(CollegesDO::getCollegeNumber, reqVO.getCollegeNumber())
                .likeIfPresent(CollegesDO::getCollegeName, reqVO.getCollegeName())
                .eqIfPresent(CollegesDO::getCollegeCode, reqVO.getCollegeCode())
                .eqIfPresent(CollegesDO::getUserId, reqVO.getUserId())
                .betweenIfPresent(CollegesDO::getOpeningTime, reqVO.getOpeningTime())
                .orderByDesc(CollegesDO::getCreateTime));
    }


    @DataPermission(enable = false)
    default List<ClassesInfoDO> selectClassesIdByCollege(Collection<String> collegeId) {
        MPJLambdaWrapper<CollegesDO> classesDOMPJLambdaWrapper = new MPJLambdaWrapperX<CollegesDO>()
                .in(CollegesDO::getId, collegeId)
                .leftJoin(DepartmentsDO.class, DepartmentsDO::getCollegeId, CollegesDO::getId)
                .leftJoin(ProfessionsDO.class, ProfessionsDO::getDepartmentsId, DepartmentsDO::getId)
                .leftJoin(ClassesDO.class, ClassesDO::getProfessionId, ProfessionsDO::getId)
                .selectAs(ClassesDO::getId, ClassesInfoDO::getClassesId)
                .selectAs(ProfessionsDO::getId, ClassesInfoDO::getProfessionsId)
                .selectAs(DepartmentsDO::getId, ClassesInfoDO::getDepartmentsId)
                .selectAs(CollegesDO::getId, ClassesInfoDO::getCollegeId)
                .selectAs(ClassesDO::getClassesName, ClassesInfoDO::getClassesName)
                .selectAs(ProfessionsDO::getProfessionName, ClassesInfoDO::getProfessionsName)
                .selectAs(DepartmentsDO::getDepartmentName, ClassesInfoDO::getDepartmentsName)
                .selectAs(ProfessionsDO::getHierarchy, ClassesInfoDO::getHierarchy)
                .selectAs(CollegesDO::getCollegeName, ClassesInfoDO::getCollegeName);

        return selectJoinList(ClassesInfoDO.class, classesDOMPJLambdaWrapper);
    }


    @DataPermission(enable = false)
    default List<ClassesInfoDO> selectClassesIdByProfession(Collection<String> professionIds) {
        MPJLambdaWrapper<CollegesDO> classesDOMPJLambdaWrapper = new MPJLambdaWrapperX<CollegesDO>()
                .in(ClassesDO::getProfessionId, professionIds)
                .leftJoin(DepartmentsDO.class, DepartmentsDO::getCollegeId, CollegesDO::getId)
                .leftJoin(ProfessionsDO.class, ProfessionsDO::getDepartmentsId, DepartmentsDO::getId)
                .leftJoin(ClassesDO.class, ClassesDO::getProfessionId, ProfessionsDO::getId)
                .selectAs(ClassesDO::getId, ClassesInfoDO::getClassesId)
                .selectAs(ProfessionsDO::getId, ClassesInfoDO::getProfessionsId)
                .selectAs(DepartmentsDO::getId, ClassesInfoDO::getDepartmentsId)
                .selectAs(CollegesDO::getId, ClassesInfoDO::getCollegeId)
                .selectAs(ClassesDO::getClassesName, ClassesInfoDO::getClassesName)
                .selectAs(ProfessionsDO::getProfessionName, ClassesInfoDO::getProfessionsName)
                .selectAs(DepartmentsDO::getDepartmentName, ClassesInfoDO::getDepartmentsName)
                .selectAs(ProfessionsDO::getHierarchy, ClassesInfoDO::getHierarchy)
                .selectAs(CollegesDO::getCollegeName, ClassesInfoDO::getCollegeName);

        return selectJoinList(ClassesInfoDO.class, classesDOMPJLambdaWrapper);
    }


}
